<?
/****************************************/
/************* IdeaLAN LLC **************/
/* Todo new better improved so DJone    */
/* Can remeber all of his great ideas.  */
/*                                      */
/****************************************/
/****************************************/
/* SCRIPT: todo.php                     */
/* AUTHOR: Lukasz Piekarski             */
/* E-mail: lukasz@idealan.net           */
if (file_exists("todo.php")) {	$mod_lud = filemtime("todo.php"); }		
$mod_ver	=	"0.1b";
/****************************************/
/*
TODO	+
		- ADD IP LOOKUP.
END TODO
*/


/* ----------- INSTALLATION ----------- */

if ($_GET["view"] == "install") {
	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");

	$check_module = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_name` = 'ToDo' AND `mod_location` = 'todo.php' AND `mod_ver` = '$mod_ver'";  
	$execute_check_module = mysql_query($check_module); 
	
	if ($execute_check_module != "") {
		if (mysql_num_rows($execute_check_module) == 0) { 
			
			
			/* *** CREATE MAIN TODO TABLE *** */
			$table_todo = "CREATE TABLE `".$DBprefix."todo` (
			`td_id` SMALLINT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
			`td_uid` SMALLINT(5) NOT NULL ,
			`td_cat` VARCHAR(100) NOT NULL ,
			`td_title` VARCHAR(50) NOT NULL ,
			`td_description` TEXT NOT NULL ,
			`td_addate` VARCHAR(50) NOT NULL ,
			`td_findate` VARCHAR(50) NOT NULL ,
			`td_progress` VARCHAR(10) NOT NULL,
			`td_status` VARCHAR(10) NOT NULL ,
			`td_rank` SMALLINT(5) NOT NULL)";

			/* ---------- LET'S CREATE THE TABLE ---------- */
			$create = mysql_query($table_todo);
	
			/* LET'S DISPLAY RESULTS FROM CREATING TABLE */
			if ($create != 1) { echo "CREATING TABLE: <i><b>TODO</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> ". mysql_errno() . " - " .  mysql_error() ."<br /><a href='../../in.php?show=config&view=modules'>Go Back</a>"; die();}


			/* *** CREATE TODO DETAIL TABLE *** */
			$table_tdd = "CREATE TABLE `".$DBprefix."todo_details` (
			`tdd_id` SMALLINT(5) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
			`tdd_tdid` SMALLINT(5) NOT NULL,
			`tdd_uid` SMALLINT(5) NOT NULL ,
			`tdd_description` TEXT NOT NULL ,
			`tdd_addate` VARCHAR(50) NOT NULL ,
			`tdd_status` VARCHAR(10) NOT NULL ,
			`tdd_rank` SMALLINT(5) NOT NULL)";

			/* ---------- LET'S CREATE THE TABLE ---------- */
			$create = mysql_query($table_tdd);
	
			/* LET'S DISPLAY RESULTS FROM CREATING TABLE */
			if ($create != 1) { echo "CREATING TABLE: <i><b>TODO DETAIL</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> ". mysql_errno() . " - " .  mysql_error() ."<br /><a href='../../in.php?show=config&view=modules'>Go Back</a>"; die();}


			
 
			/* ---------- ADD MODULE TO MODULES LIST---------- */			
			// LETS CHECK IF THERE ARE ANY MODULES INSTALLED IF NOT LETS MAKE THIS ONE #1
			$get_modules = "SELECT * FROM `".$DBprefix."modules` ORDER BY `mod_id` DESC";  
			$execute_get_modules = mysql_query($get_modules); 
			
			$row_count = mysql_num_rows($execute_get_modules);
			
			if ($row_count != 0) {
			if ($module = mysql_fetch_array($execute_get_modules)){

				$mod_id = $module["mod_id"];
				$mod_name = $module["mod_name"];
				$mod_location = $module["mod_location"];
			
			}
			
			$add_module = "INSERT INTO `".$DBprefix."modules` (`mod_id`,`mod_name`,`mod_location`,`mod_ver`,`mod_lud`) VALUES ('$row_count' + 1,'ToDo','todo.php','$mod_ver','$mod_lud')";
			$add_result = mysql_query($add_module);
			
			} else {

			$add_module = "INSERT INTO `".$DBprefix."modules` (`mod_id`,`mod_name`,`mod_location`,`mod_ver`,`mod_lud`) VALUES ('1','ToDo','todo.php','$mod_ver','$mod_lud')";
			$add_result = mysql_query($add_module);
			
			}
			


				
			header("Location: ../../in.php?show=config&view=modules&m=20");
			die();
				
		} else {
			echo "ADDING MODULE: <i><b>ToDo</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> Module is already added.<br />";
			
			echo "<a href='../../in.php?show=config&view=modules'>Go Back</a>";
			
			die(); 
		} // END COUNT ROWS 
	} 
} // END INSTALL





/* -------- END INSTALLATION --------- */

/* ----------- REMOVAL ----------- */

if ($_GET["view"] == "remove") {
	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");

	$check_module = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_name` = 'ToDo' AND `mod_location` = 'todo.php'";  
	$execute_check_module = mysql_query($check_module); 
	
	if ($execute_check_module != "") {
		if (mysql_num_rows($execute_check_module) > 0) { 
			
			
			/* *** DROP MAIN TODO TABLE *** */
			$table_todo = "DROP TABLE `".$DBprefix."todo`";

			/* ---------- LET'S DROP THE TABLE ---------- */
			$drop = mysql_query($table_todo);
	
			/* LET'S DISPLAY RESULTS FROM DROPING TABLE */
			if ($drop != 1) { echo "REMOVING TABLE: <i><b>TODO</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> ". mysql_errno() . " - " .  mysql_error() ."<br /><a href='../../in.php?show=config&view=modules'>Go Back</a>"; die();}


			/* *** DROP TODO DETAILS TABLE *** */
			$table_tdd = "DROP TABLE `".$DBprefix."todo_details`";

			/* ---------- LET'S DROP THE TABLE ---------- */
			$drop = mysql_query($table_tdd);
	
			/* LET'S DISPLAY RESULTS FROM DROPING TABLE */
			if ($drop != 1) { echo "REMOVING TABLE: <i><b>TODO DETAILS</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> ". mysql_errno() . " - " .  mysql_error() ."<br /><a href='../../in.php?show=config&view=modules'>Go Back</a>"; die();}
			
 
			/* ---------- REMOVE MODULE FROM MODULES ---------- */	
			$remove_module = "DELETE FROM `".$DBprefix."modules` WHERE `mod_name` = 'ToDo' AND `mod_location` = 'todo.php'";
			$remove_result = mysql_query($remove_module);
			
			$get_modules = "SELECT * FROM `".$DBprefix."modules` ORDER BY `mod_id` ASC";  
			$execute_get_modules = mysql_query($get_modules); 
			
			$row_count = mysql_num_rows($execute_get_modules);
			
			while ($module = mysql_fetch_array($execute_get_modules)){
				$i = $i + 1;
				
				$mod_id = $module["mod_id"];
				$mod_name = $module["mod_name"];
				$mod_location = $module["mod_location"];
				
			
			$update_modules = "UPDATE  `".$DBprefix."modules` SET  `mod_id` = '$i'  WHERE `mod_location` = '$mod_location'";
			$execute_upodate_modules = mysql_query($update_modules);
			
			}
			
				
			header("Location: ../../in.php?show=config&view=modules&m=21");
			die();
				
		} else {
			echo "REMOVING MODULE: <i><b>TODO</b></i>........ <font color='#FF0000'><b>ERROR!</b></font> Module is already gone.<br />";
			
			echo "<a href='../../in.php?show=config&view=modules'>Go Back.</a>";
			
			die(); 
		} // END COUNT ROWS 
	} 
} // END REMOVAL

/* ----------   BACK UP MODULE ----------- */
if ($_GET["view"] == "backup") {

	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");	

	$backup_file = "ToDo_". date("d-m-Y_H-i-s")  . ".gz";
	$backup = "mysqldump --opt -h $server -u$username -p$password $database $DBprefix"."todo | gzip > $backup_file";
	system($backup);
	
	
	header("Location: ../../in.php?show=config&view=modules&m=22");
	die();	
	
} // END BACKUP


/* -----------  RESTORE MODULE ------------- */

if ($_GET["view"] == "restore") {

	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");
	
	$restore_file = $_GET["file"];
	$restore = "zcat $restore_file | mysql -u$username -p$password $database";
	system($restore);
	
	
	header("Location: ../../in.php?show=config&view=modules&m=25");
	die();	
	
} // END RESTORE

/* --------- CHANGE ORDER ------------ */
if ($_GET["view"] == "order") {

	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");
	
	if ($_GET["change"] == "down") {
	
		$new_order = $_GET["modid"] + 1;
		
		$get_module = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_id` = '$new_order'";  
		$execute_get_module = mysql_query($get_module); 
			
			if ($module = mysql_fetch_array($execute_get_module)){
				
				$mod_id = $module["mod_id"];
				$mod_name = $module["mod_name"];
				$mod_location = $module["mod_location"];
				
				$update_order2 = "UPDATE `".$DBprefix."modules` SET `mod_id` = '". $_GET["modid"] ."' WHERE `mod_name` = '$mod_name' AND `mod_location` =  '$mod_location' LIMIT 1";
				$update_result = mysql_query($update_order2);
		
			}
			
		$update_order1 = "UPDATE `".$DBprefix."modules` SET `mod_id` = '$new_order' WHERE `mod_name` = 'ToDo' AND `mod_location` =  'todo.php' LIMIT 1";
		$update_result = mysql_query($update_order1);
	
	
		header("Location: ../../in.php?show=config&view=modules&m=31");
		die();
	}

	if ($_GET["change"] == "up") {

		$new_order = $_GET["modid"] - 1;
		
		$get_module = "SELECT * FROM `".$DBprefix."modules` WHERE `mod_id` = '$new_order'";  
		$execute_get_module = mysql_query($get_module); 
			
			if ($module = mysql_fetch_array($execute_get_module)){
				
				$mod_id = $module["mod_id"];
				$mod_name = $module["mod_name"];
				$mod_location = $module["mod_location"];
				
				$update_order2 = "UPDATE `".$DBprefix."modules` SET `mod_id` = '". $_GET["modid"] ."' WHERE `mod_name` = '$mod_name' AND `mod_location` =  '$mod_location' LIMIT 1";
				$update_result = mysql_query($update_order2);
		
			}
			
		$update_order1 = "UPDATE `".$DBprefix."modules` SET `mod_id` = '$new_order' WHERE `mod_name` = 'ToDo' AND `mod_location` =  'todo.php' LIMIT 1";
		$update_result = mysql_query($update_order1);
	
	
		header("Location: ../../in.php?show=config&view=modules&m=32");
		die();
	
	}

} // END CHANGE ORDER

/* --------- UPDATE VERSION ------------ */

if ($_GET["view"] == "verup") {

	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");
	

	$update_modules = "UPDATE  `".$DBprefix."modules` SET  `mod_ver` = '$mod_ver',`mod_lud` = '$mod_lud' WHERE `mod_location` = 'todo.php'";
	$execute_upodate_modules = mysql_query($update_modules);
	
			
	header("Location: ../../in.php?show=config&view=modules&m=33");
	die();
			
} // END UPDATE VERSION

/* -------- END INSTALATION --------- */


if ($_GET["view"] == "modify") {

	$config_path = "../../../";
	$cFile = $config_path . "addons/config.ini";
	include ($config_path . "config.php");

	$modid = $_POST["modid"] . $_GET["modid"];

	if ($_POST["do"] == "add") {
		// DO ADD SERVER
		
		$server_name = $_POST["server_name"];
		$server_custid = $_POST["cust_ID"];
		$server_addby = $_POST["server_addby"];
		
		$server_auth = base64_encode($timedate);
		
		//echo "$server_name : $server_custid : $server_addby";
		$add_server = "INSERT INTO `".$DBprefix."servers` (`server_name` ,`server_auth` ,`server_custid` ,`server_upcount` ,`server_addby`) VALUES ('$server_name', '$server_auth', '$server_custid', '0', '$server_addby')";  
		$execute_add_server = mysql_query($add_server);

		header("Location: ../../in.php?show=mod&modid=$modid&m=26");
		die();	


	} // END DO ADD SERVER

	if ($_GET["do"] == "del") {
		// DELETE SERVER
		$server_id = $_GET["sid"];
		
		// DELETE SERVER
		$delete_server = "DELETE FROM `".$DBprefix."servers` WHERE `server_id` = '$server_id'";  
		$execute_delete_server = mysql_query($delete_server);

		header("Location: ../../in.php?show=mod&modid=$modid&m=27");
		die();	

	} // END DELETE SERVER

} // END MODIFY
	
	
	
	


if (empty($_SESSION["sesid"])) { header ("Location: ../../in.php?e=0"); die();}
?>

		<div id="content">
					<div id="left">
							<div class="naglowek"><div class="pkt"></div>ToDo Organizer</div>
							<ul id="me">
									<li <? if ($view == "") { echo "id=\"press\""; } ?> ><a href="in.php?show=mod&modid=<? echo $modid; ?>">Active Tasks</a></li>
									<li <? if ($view == "completed") { echo "id=\"press\" "; } ?> ><a href="in.php?show=mod&view=completed&modid=<? echo $modid; ?>">Completed Tasks</a></li>
									<li <? if ($view == "add") { echo "id=\"press\" "; } ?> ><a href="in.php?show=mod&view=add&modid=<? echo $modid; ?>">Add new task</a></li>

							</ul>
					
					
					</div>					
					
					<div id="right">
<?
if (!$view) {

// ATTACHE THIS TO THE PAGE, YOU WANT THE NEXT LINK AND PREVIOUS TO BE DISPLAYED
$result = mysql_query("SELECT * FROM `".$DBprefix."servers` ORDER BY `server_id` DESC"); 
$rownumbers = mysql_num_rows($result);
$linktest ="in.php?show=mod&modid=$modid";


include "inc/pages.php";


// LETS GET SERVERS FROM DATABASE

if ($sq == "") {
	$get_servers = "SELECT * FROM `".$DBprefix."servers` ORDER BY `server_id` DESC LIMIT $begin,20";
} else {
	$get_servers = "SELECT * FROM `".$DBprefix."servers` WHERE `server_name` LIKE '%$sq%' OR `server_custid` LIKE '%$sq%' ORDER BY `server_id` DESC LIMIT $begin,20"; 
}

$execute_get_servers = mysql_query($get_servers); 
$count_servers = mysql_num_rows($execute_get_servers);

echo "
						<div class='naglowek'><div class='pkt'></div>ACTIVE TASKS</div>
							<div id='rightcont'>
<form method='POST' action='in.php?show=mod&modid=$modid'>

<center><lable>SEARCH: </lable><input name=\"sq\" type=\"text\" class=\"input_form1\"> <input type=\"submit\" value=\"SEARCH\" class=\"button1\"></center><br />

</form>
								<table class='editform1' style='width: 100%'>
       	 <tr>
	  		<td width='100' class='tableLogsHeader'>TYPE</td>
			<td width='300' class='tableLogsHeader'>TITLE</td>
  			<td width='100' class='tableLogsHeader'>TIME LEFT</td>
  			<td width='150' class='tableLogsHeader'>%</td>
  			<td width='100' class='tableLogsHeader'>OWNER</td>
  			<td width='100' class='tableLogsHeader'>OPTIONS</td>
		</tr>
";

while ($server = mysql_fetch_array($execute_get_servers)){
$server_id = $server["server_id"];
$server_name = $server["server_name"];
$server_auth = $server["server_auth"];
$server_ip = $server["server_ip"];
$server_custid = $server["server_custid"];
$server_upcount = $server["server_upcount"];
$server_lastupdate = $server["server_lastupdate"];
$server_addby = $server["server_addby"];


$server_green = $server_lastupdate + 600;
$server_yellow = $server_lastupdate + 900;
$server_red = $server_lastupdate + 1200;

if ($timedate <= $server_green) {
$server_status = "<img src='../g/ico/flag_green.png'>";

} else if ($timedate >= $server_yellow AND $timedate <= $server_red) {
$server_status = "<img src='../g/ico/flag_yellow.png'>";

} else if ($timedate >= $server_red) {
$server_status = "<img src='../g/ico/flag_red.png'>";

}








echo "<script type=\"text/javascript\">
function show_alert$server_id()
{
alert(\"*/10 * * * * lynx -dump http://office.idealan.us/members/inc/modules/servers.php?view=check&auth=$server_auth >/dev/null 2>&1\");
}
</script>


       	 <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">
	  		<td class='tableLogs'>&nbsp;$server_id</td>
	  		<td class='tableLogs'>&nbsp;$server_name</td>
  			<td class='tableLogs'>&nbsp;$server_auth</td>
  			<td class='tableLogs'>&nbsp;$server_ip</td>
  			<td class='tableLogs'>&nbsp;$server_status </td>
			<td class='tableLogs'>$extra_links <a href='#' onclick=\"show_alert$server_id()\"><img src='../g/ico/server_link.png'></a> <a href=\"javascript:confirmDelete('inc/modules/servers.php?view=modify&do=del&sid=$server_id&modid=$modid','DELETING: $server_name')\"><img src='../g/ico/server_delete.png'></a> <a target=\"_BLANK\" href=\"https://$server_ip:10000/\"><img src='../g/ico/server_connect.png'></a></td>
		</tr>
";
}


echo "
</table><br />
<center>$link_prev $pagejumpline $link_next</center>
							</div>
";


}
 // END ACTIVE TASKS

if ($view == "completed") {

echo "
							<div class='naglowek'><div class='pkt'></div>COMPLETED TASKS</div>
							<div id='rightcont'>


";

} // END COMPLETED TASKS

if ($view == "add") {

echo "
							<div class='naglowek'><div class='pkt'></div>ADD NEW TASK</div>
							<div id='rightcont'>
<form method='POST' action='inc/modules/servers.php?view=modify'>
<input type='hidden' name='do' value='add' />
<input type='hidden' name='server_addby' value='$info_user_id' />
<table style='width: 100%' class='editform1'>
	<tr>
		<td style='width: 145px' class='table1'>&nbsp;</td>
		<td class='table2'>&nbsp;</td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>Type:</td>
		<td class='table3'><select name='td_type' class='input_form'><option value='1'>To-Do</option><option value='2'>Call</option><option value='3'>Meeting</option><option value='4'>Script</option><option value='5'>Note</option></select></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>Title:</td>
		<td class='table3'><input class='input_form' type=\"text\" size=\"23\" name=\"td_title\" value=\"\" autocomplete='off'></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>By date:</td>
		<td class='table3'><input class='input_form' type='text' name='fso_opend' value='$open_date' readonly> <img class='calChange' src=\"../g/ico/calendar.png\" onclick=\"displayCalendar(document.forms[0].fso_opend,'mm.dd.yyyy',this)\" /></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>By time:</td>
		<td class='table3'></td>
	</tr>
	<tr>
		<td style='width: 145px' class='table1'>Description:</td>
		<td class='table3'><textarea class='textarea_form'> </textarea></td>
	</tr>

	<tr>
		<td style='width: 145px' class='table1'>&nbsp;</td>
		<td class='table2'>&nbsp;</td>
	</tr>

</table>
<p>&nbsp;</p>

    <input type=\"submit\" class=\"button1\" value=\"SAVE\">
</form>

";

} // END ADD TASK


?>

</div>
